//防逆流MOS管控制
void backflowControl(){
  if(system_PCBA_mode==0){
    digitalWrite(ESP_PIN_BACKFLOW_MOS, HIGH);
  }else{
    if(system_ina_voltage_1 > system_ina_voltage_2 + system_voltage_dropout){
      digitalWrite(ESP_PIN_BACKFLOW_MOS, HIGH);
    }else{
      digitalWrite(ESP_PIN_BACKFLOW_MOS, LOW);
    }
  }
}

//硬件保护
void Device_Protection(){
  system_error = 0;
  backflowControl();
  //过温保护
  if(system_temperature_ntc > system_protect_TEM){
    system_error ++;
    system_info_marker |= 0xFF00;
  }else{
    system_info_marker &= 0xFEFF;
  }
  //主板过流保护
  if(system_ina_current_1 > PCBA_CURRENT_LIMIT || system_ina_current_2 > PCBA_CURRENT_LIMIT){
    system_error ++;
    system_info_marker |= 0xFE80;
  }else{
    system_info_marker &= 0xFF7F;
  }
  //电池过流保护
  //1、立即反应
  if(system_ina_current_2 > system_bat_current_limit*system_bat_current_margin){
    system_error ++;
    system_info_marker |= 0xFE40;
  }
  //2、延时反应
  else if(system_ina_current_2 > system_bat_current_limit){
    if(system_batcur_clocker==0){
      system_batcur_clocker = millis();
    }
    if(millis()-system_batcur_clocker>=system_batcur_time){
      system_error ++;
      system_info_marker |= 0xFE40;
    }
  }else{
    system_info_marker &= 0xFFBF;
    system_batcur_clocker = 0;
  }
  //过压保护
  //1、立即反应
  if(system_ina_voltage_2 > system_bat_maxv*system_bat_voltage_margin || system_ina_voltage_1 > PCBA_VOLTAGE_LIMIT || system_ina_voltage_2 > PCBA_VOLTAGE_LIMIT){
    system_error ++;
    system_info_marker |= 0xFE20;
  }
  //2、延时反应
  else if(system_ina_voltage_2 > system_bat_maxv){
    if(system_batvol_clocker==0){
      system_batvol_clocker = millis();
    }
    if(millis()-system_batvol_clocker>=system_batvol_time){
      system_error ++;
      system_info_marker |= 0xFE20;
    }
  }else{
    system_info_marker &= 0xFFDF;
    system_batvol_clocker = 0;
  }
  //低于最低电压
  if(system_ina_voltage_1 < system_voltage_min && system_ina_voltage_2 < system_voltage_min){
    system_error ++;
    system_info_marker |= 0xFE10;
  }else{
    system_info_marker &= 0xFFEF;
  }
  //电池保护，低于保护电压进入保护状态，高于保护电压+0.5时恢复
  if(system_ina_voltage_2 < system_bat_protectv){
    system_info_marker |= 0xFE08;
    digitalWrite(ESP_PIN_OUTPUT, HIGH);
  }else if(((system_info_marker>>3)&0x1) && system_ina_voltage_2 > system_bat_protectv + 0.5){
    system_info_marker &= 0xFFF7;
  }

  if(system_PCBA_mode == 1){ 
    //MPPT模式
    //IUV = 0;
    backflowControl(); 
    //电池电压低于最小电池电压？
    if(system_ina_voltage_2 < system_bat_minv){
      //system_error ++;
      system_info_marker |= 0xFE02;
    }else{
      system_info_marker &= 0xFFFD;
    }
    //输入电压低于电池电压？
    if(system_ina_voltage_1 < system_ina_voltage_2 + system_voltage_dropout){
      system_error ++;
      system_info_marker |= 0xFE04;
    }else{
      if(system_info_marker & 0x4){ system_REC = 1; } 
      system_info_marker &= 0xFFFB;
    }
  }
  
  //散热启动
  if(system_temperature_ntc > system_fan_fanOpen_TEM){
    system_info_marker |= 0xFE01;
    digitalWrite(ESP_PIN_FAN, HIGH);
  }else if(system_temperature_ntc < system_fan_fanOpen_TEM - 2){
    system_info_marker &= 0xFFFE;
    digitalWrite(ESP_PIN_FAN, LOW);
  }

  //输出错误日志到MQTT服务器
  if(system_info_marker != system_info_marker_before){
    system_info_marker_before = system_info_marker;
    MQTT_send_log(getSysInfo());
  }
}
//错误日志
String getErrorInfo(){
  //打印错误信息
  String msg_buffer = "";
  if((system_info_marker>>8)&0x1){
    msg_buffer += "高温  ";
  }
  if((system_info_marker>>7)&0x1){
    msg_buffer += "输入过流  ";
  }
  if((system_info_marker>>6)&0x1){
    msg_buffer += "输出过流  ";
  }
  if((system_info_marker>>5)&0x1){
    msg_buffer += "输出超压  ";
  }
  if((system_info_marker>>4)&0x1){
    msg_buffer += "低电压  ";
  }
  if((system_info_marker>>3)&0x1){
    msg_buffer += "电池输出保护  ";
  }
  if((system_info_marker>>2)&0x1){
    msg_buffer += "输入电压低于电池电压  ";
  }
  if((system_info_marker>>1)&0x1){
    msg_buffer += "电池电压过低  ";
  }
  return msg_buffer;
}
//输出运行信息
String getSysInfo(){
    String msg_buffer = getErrorInfo();
    if(system_info_marker&0x1){
      msg_buffer += "散热风扇启动  ";
    }
    if(msg_buffer.length()>1){return msg_buffer;}else{return "正常运行…";}
}

